#!/usr/bin/perl -w
#
# $Id: Preferences.pm,v 1.29 2005/04/11 21:58:16 solovam Exp $
#
# This file is a part of gkismet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#

#
# Preferences class
#
package Preferences;

use strict;
use Misc;
use Observable;

@Preferences::ISA = qw(Observable);

#
# Constructor
#
sub new
{
	my $type = shift;
	my $self = {};
	bless $self, $type;

	$self->{'gKismetApplication'} = shift;

	# Network fields
	$self->{'networkFields'} =
		[
			{field => 'BSSID', show => 1},
			{field => 'Beacon info', show => 1},
			{field => 'Carrier', show => 1},
			{field => 'Encoding', show => 1},
			{field => 'Manufacturer', show => 1},
			{field => 'Model', show => 1},
			{field => 'Max rate', show => 1},
			{field => 'Max seen', show => 1},
			{field => 'First seen', show => 1},
			{field => 'Last seen', show => 1},
			{field => 'Clients', show => 1},
			{field => 'Type', show => 1},
			{field => 'Channel', show => 1},
			{field => 'WEP', show => 1},
			{field => 'Encryption', show => 1},
			{field => 'Decrypted', show => 1},
			{field => 'Beacon',  show => 1},
			{field => 'Data packets', show => 1},
			{field => 'LLC packets', show => 1},
			{field => 'Crypt packets', show => 1},
			{field => 'Weak packets', show => 1},
			{field => 'Dupe IV', show => 1},
			{field => 'Data', show => 1},
			{field => 'Power', show => 1},
			{field => 'Noise', show => 1},
			{field => 'IP Range', show => 1},
			{field => 'IP Type', show => 1}
		];
	for my $f (@{$self->{'networkFields'}})
	{
		$f->{'show'} = Gnome::Config->get_bool('/gkismet/networkFields/' . $f->{'field'} . '=true');
	}
	# Client fields
	$self->{'clientFields'} =
		[
			{field => 'Type', show => 1},
			{field => 'Manufacturer', show => 1},
			{field => 'Model', show => 1},
			{field => 'First seen', show => 1},
			{field => 'Last seen', show => 1},	
			{field => 'Max rate', show => 1},
			{field => 'Max seen', show => 1},
			{field => 'Encoding', show => 1},
			{field => 'Channel', show => 1},
			{field => 'WEP', show => 1},
			{field => 'Decrypted', show => 1},
			{field => 'IP', show => 1},
			{field => 'Data packets', show => 1},
			{field => 'Crypt packets', show => 1},
			{field => 'Weak packets', show => 1},
			{field => 'Data', show => 1},
			{field => 'Power', show => 1},
			{field => 'Noise', show => 1}
		];
	for my $f (@{$self->{'clientFields'}})
	{
		$f->{'show'} = Gnome::Config->get_bool('/gkismet/clientFields/' . $f->{'field'} . '=true');
	}

	# Window depth
	$self->{'packetDepth'} = Gnome::Config->get_int('/gkismet/misc/packetDepth=100');
	$self->{'stringDepth'} = Gnome::Config->get_int('/gkismet/misc/stringDepth=100');

	# Measures
	$self->{'units'} = Gnome::Config->get_string('/gkismet/misc/units=imperial');

	# Keep network tree sorted
	$self->{'keepSorted'} = Gnome::Config->get_bool('/gkismet/misc/keepSorted=false');

	# Sound
	my $soundDir = $self->{'gKismetApplication'}{'path'}{'sound'};
	$self->{'playBinary'} = Gnome::Config->get_string('/gkismet/sound/playBinary=/usr/bin/play');
	$self->{'networkSoundEnabled'} = Gnome::Config->get_bool('/gkismet/sound/NetworkSoundEnabled=true');
	$self->{'gpsSoundEnabled'} = Gnome::Config->get_bool('/gkismet/sound/gpsSoundEnabled=true');
	$self->{'trafficSoundEnabled'} = Gnome::Config->get_bool('/gkismet/sound/trafficSoundEnabled=false');
	$self->{'alertSoundEnabled'} = Gnome::Config->get_bool('/gkismet/sound/alertSoundEnabled=true');
	$self->{'newNetworkSound'} = Gnome::Config->get_string('/gkismet/sound/newNetworkSound=' . $soundDir . "/new_network.wav");
	$self->{'gpsAcqSound'} = Gnome::Config->get_string('/gkismet/sound/gpsAcqSound=' . $soundDir . "/alert.wav");
	$self->{'gpsLostSound'} = Gnome::Config->get_string('/gkismet/sound/gpsLostSound=' . $soundDir . "/alert.wav");
	$self->{'trafficSound'} = Gnome::Config->get_string('/gkismet/sound/trafficSound=' . $soundDir . "/traffic.wav");
	$self->{'junkTrafficSound'} = Gnome::Config->get_string('/gkismet/sound/junkTrafficSound=' . $soundDir . "/junk_traffic.wav");
	$self->{'alertSound'} = Gnome::Config->get_string('/gkismet/sound/alertSound=' . $soundDir . "/alert.wav");

	return $self;
}

#
# Save preferences
#
sub save
{
	my $self = shift;

	for my $f (@{$self->{'networkFields'}})
	{
		Gnome::Config->set_bool('/gkismet/networkFields/' . $f->{'field'}, $f->{'show'});
	}
	for my $f (@{$self->{'clientFields'}})
	{
		Gnome::Config->set_bool('/gkismet/clientFields/' . $f->{'field'}, $f->{'show'});
	}
	Gnome::Config->set_int('/gkismet/misc/packetDepth', $self->{'packetDepth'});
	Gnome::Config->set_int('/gkismet/misc/stringDepth', $self->{'stringDepth'});
	Gnome::Config->set_string('/gkismet/misc/units', $self->{'units'});
	Gnome::Config->set_bool('/gkismet/misc/keepSorted', $self->{'keepSorted'});
	Gnome::Config->set_string('/gkismet/sound/playBinary', $self->{'playBinary'});
	Gnome::Config->set_bool('/gkismet/sound/networkSoundEnabled', $self->{'networkSoundEnabled'});
	Gnome::Config->set_bool('/gkismet/sound/gpsSoundEnabled', $self->{'gpsSoundEnabled'});
	Gnome::Config->set_bool('/gkismet/sound/trafficSoundEnabled', $self->{'trafficSoundEnabled'});
	Gnome::Config->set_bool('/gkismet/sound/alertSoundEnabled', $self->{'alertSoundEnabled'});
	Gnome::Config->set_string('/gkismet/sound/newNetworkSound', $self->{'newNetworkSound'});
	Gnome::Config->set_string('/gkismet/sound/gpsAcqSound', $self->{'gpsAcqSound'});
	Gnome::Config->set_string('/gkismet/sound/gpsLostSound', $self->{'gpsLostSound'});
	Gnome::Config->set_string('/gkismet/sound/trafficSound', $self->{'trafficSound'});
	Gnome::Config->set_string('/gkismet/sound/junkTrafficSound', $self->{'junkTrafficSound'});
	Gnome::Config->set_string('/gkismet/sound/alertSound', $self->{'alertSound'});
	Gnome::Config->sync();
	$self->notifyObservers($self);
}

#
# Get a prefernce
#
sub getPref
{
	my $self = shift;
	my $pref = shift;

	if($pref eq 'networkFieldNames')
	{
		my @fieldNames;
		for my $f (@{$self->{'networkFields'}})
		{
			push @fieldNames, $f->{'field'};
		}
		return @fieldNames;
	}
	elsif($pref eq 'clientFieldNames')
	{
		my @fieldNames;
		for my $f (@{$self->{'clientFields'}})
		{
			push @fieldNames, $f->{'field'};
		}
		return @fieldNames;
	}
	elsif($pref eq 'networkFieldStatus')
	{
		my $field = shift;
		for my $f (@{$self->{'networkFields'}})
		{
			if($f->{'field'} eq $field)
			{
				return $f->{'show'};
			}
		}
		return 0;
	}
	elsif($pref eq 'clientFieldStatus')
	{
		my $field = shift;
		for my $f (@{$self->{'clientFields'}})
		{
			if($f->{'field'} eq $field)
			{
				return $f->{'show'};
			}
		}
		return 0;
	}
	elsif($pref eq 'packetDepth')
	{
		return $self->{'packetDepth'};
	}
	elsif($pref eq 'stringDepth')
	{
		return $self->{'stringDepth'};
	}
	elsif($pref eq 'units')
	{
		return $self->{'units'};
	}
	elsif($pref eq 'keepSorted')
	{
		return $self->{'keepSorted'};
	}
	elsif($pref eq 'playBinary')
	{
		return $self->{'playBinary'};
	}
	elsif($pref eq 'networkSoundEnabled')
	{
		return $self->{'networkSoundEnabled'};
	}
	elsif($pref eq 'gpsSoundEnabled')
	{
		return $self->{'gpsSoundEnabled'};
	}
	elsif($pref eq 'trafficSoundEnabled')
	{
		return $self->{'trafficSoundEnabled'};
	}
	elsif($pref eq 'alertSoundEnabled')
	{
		return $self->{'alertSoundEnabled'};
	}
	elsif($pref eq 'newNetworkSound')
	{
		return $self->{'newNetworkSound'};
	}
	elsif($pref eq 'gpsAcqSound')
	{
		return $self->{'gpsAcqSound'};
	}
	elsif($pref eq 'gpsLostSound')
	{
		return $self->{'gpsLostSound'};
	}
	elsif($pref eq 'trafficSound')
	{
		return $self->{'trafficSound'};
	}
	elsif($pref eq 'junkTrafficSound')
	{
		return $self->{'junkTrafficSound'};
	}
	elsif($pref eq 'alertSound')
	{
		return $self->{'alertSound'};
	}
	else
	{
		return undef;
	}
}

#
# Set a preference
#
sub setPref
{
	my $self = shift;
	my $pref = shift;

	if($pref eq 'networkFieldStatus')
	{
		my $field = shift;
		my $status = shift;
		for my $f (@{$self->{'networkFields'}})
		{
			if($f->{'field'} eq $field)
			{
				$f->{'show'} = $status;
			}
		}
		return 0;
	}
	elsif($pref eq 'clientFieldStatus')
	{
		my $field = shift;
		my $status = shift;
		for my $f (@{$self->{'clientFields'}})
		{
			if($f->{'field'} eq $field)
			{
				$f->{'show'} = $status;
			}
		}
		return 0;
	}
	elsif($pref eq 'packetDepth')
	{
		my $depth = shift;
		$self->{'packetDepth'} = $depth;
	}
	elsif($pref eq 'stringDepth')
	{
		my $depth = shift;
		$self->{'stringDepth'} = $depth;
	}
	elsif($pref eq 'units')
	{
		my $val = shift;
		$self->{'units'} = $val;
	}
	elsif($pref eq 'keepSorted')
	{
		my $val = shift;
		$self->{'keepSorted'} = $val;
	}
	elsif($pref eq 'playBinary')
	{
		my $val = shift;
		$self->{'playBinary'} = $val;
	}
	elsif($pref eq 'networkSoundEnabled')
	{
		my $val = shift;
		$self->{'networkSoundEnabled'} = $val;
	}
	elsif($pref eq 'gpsSoundEnabled')
	{
		my $val = shift;
		$self->{'gpsSoundEnabled'} = $val;
	}
	elsif($pref eq 'trafficSoundEnabled')
	{
		my $val = shift;
		$self->{'trafficSoundEnabled'} = $val;
	}
	elsif($pref eq 'alertSoundEnabled')
	{
		my $val = shift;
		$self->{'alertSoundEnabled'} = $val;
	}
	elsif($pref eq 'newNetworkSound')
	{
		my $val = shift;
		$self->{'newNetworkSound'} = $val;
	}
	elsif($pref eq 'gpsAcqSound')
	{
		my $val = shift;
		$self->{'gpsAcqSound'} = $val;
	}
	elsif($pref eq 'gpsLostSound')
	{
		my $val = shift;
		$self->{'gpsLostSound'} = $val;
	}
	elsif($pref eq 'trafficSound')
	{
		my $val = shift;
		$self->{'trafficSound'} = $val;
	}
	elsif($pref eq 'junkTrafficSound')
	{
		my $val = shift;
		$self->{'junkTrafficSound'} = $val;
	}
	elsif($pref eq 'alertSound')
	{
		my $val = shift;
		$self->{'alertSound'} = $val;
	}
}

1;
